﻿#include "chart.h"
#include "../../include/macro.h"

Chart::Chart(QGraphicsItem* parent, Qt::WindowFlags wFlags):
    QChart(QChart::ChartTypeCartesian, parent, wFlags),
    m_axisX(new QValueAxis()),
    m_axisY(new QValueAxis()),
    m_step(0),
    m_x(100)
{
    m_axisX->setTickCount(80);
    m_axisX->setRange(0, 120);
    m_axisX->setTitleText(QStringLiteral("时间(ms)"));
    m_axisY->setTitleText(QStringLiteral("电流(mA)"));
    m_axisY->setRange(AXIS_Y_LOWER, AXIS_Y_UPPER);

    const QColor color[4] = {QColor(255,100,100),QColor(100,255,100),
                             QColor(120,120,255),QColor(240,240,0)};
    addAxis(m_axisX,Qt::AlignBottom);
    addAxis(m_axisY,Qt::AlignLeft);
    for(int i=0;i<4;i++)
    {
        QPen pen(color[i]);
        pen.setWidth(2);
        m_series[i] = new QSplineSeries(this);
        m_series[i]->setPen(pen);

        addSeries(m_series[i]);
        m_series[i]->attachAxis(m_axisX);
        m_series[i]->attachAxis(m_axisY);
    }

    axis_step = (m_axisX->max() - m_axisX->min()) / m_axisX->tickCount();
    legend()->hide();
}

Chart::~Chart()
{

}

void Chart::slt_updateChart(const QList<int>& data)
{
    scroll_step = plotArea().width() / m_axisX->tickCount();
    m_x += axis_step;
    for(int i=0;i<4;i++)
        m_series[i]->append(m_x, (qreal)data[i]);
    scroll(scroll_step, 0);
}

void Chart::slt_dtuRouteSwtich(int route_id, bool checked)
{
    if(checked)
    {
        addSeries(m_series[route_id]);
        m_series[route_id]->attachAxis(m_axisX);
        m_series[route_id]->attachAxis(m_axisY);
    }
    else
    {
        removeSeries(m_series[route_id]);
    }
}


ChartManager::ChartManager(QChartView* view)
    : curChart(nullptr), chartView(view)
{   
}

ChartManager::~ChartManager()
{
    for(auto chart : chartlist)
        delete chart;
}
/**/
void ChartManager::updateChart(uint id, const QList<int>& data)
{
    //qDebug() << id ;
    chartlist[id]->slt_updateChart(data);
}

void ChartManager::slt_dtuRouteSwtich(int route_id, bool checked)
{
    curChart->slt_dtuRouteSwtich(route_id, checked);
}

void ChartManager::slt_dtuSwitch(uint id)
{
    curChart = chartlist[id];
    chartView->setChart(curChart);
}

void ChartManager::addDTU(uint id)
{
    chartlist[id] = new Chart();
    slt_dtuSwitch(id);
}


HisChart::HisChart(QGraphicsItem* parent, Qt::WindowFlags wFlags):
    QChart(QChart::ChartTypeCartesian, parent, wFlags),
    m_axisX(new QValueAxis()),
    m_axisY(new QValueAxis())
{
    m_axisX->setTitleText(QStringLiteral("时间(ms)"));
    m_axisY->setTitleText(QStringLiteral("电流(mA)"));
    m_axisX->setRange(AXIS_X_LOWER, AXIS_X_UPPER);
    m_axisY->setRange(AXIS_Y_LOWER, AXIS_Y_UPPER);

    const QColor color[4] = {QColor(255,100,100),QColor(100,255,100),
                             QColor(120,120,255),QColor(240,240,0)};
    addAxis(m_axisX,Qt::AlignBottom);
    addAxis(m_axisY,Qt::AlignLeft);
    for(int i=0;i<4;i++)
    {
        QPen pen(color[i]);
        pen.setWidth(2);
        m_series[i] = new QSplineSeries(this);
        m_series[i]->setPen(pen);

        addSeries(m_series[i]);
        m_series[i]->attachAxis(m_axisX);
        m_series[i]->attachAxis(m_axisY);
    }
    legend()->hide();
}

HisChart::~HisChart()
{

}

void HisChart::slt_dtuRouteSwtich(int route_id, bool checked)
{
    if(checked)
    {
        addSeries(m_series[route_id]);
        m_series[route_id]->attachAxis(m_axisX);
        m_series[route_id]->attachAxis(m_axisY);
    }
    else
    {
        removeSeries(m_series[route_id]);
    }
}

void HisChart::slt_updateSeries(const QList<QVector<QPointF>> &currents)
{
    if(currents[0].empty())
        QMessageBox::warning(NULL,  QStringLiteral("搜索结果为空"), QStringLiteral("该dtu此时段无数据"));
    else
    {
        for(int i=0;i<4;i++)
            m_series[i]->replace(currents[i]);
    }

}




